<?php
namespace app\index\controller;
use \think\Db;
use \think\Session;
use \think\Request;
use \think\Controller;

class forum{

		
	//获取用户填写帖子信息
	public function postInfo(){
		$request = Request::instance();

		//获取发帖人id
			$userid 	= $request->session('userid');
			$title      = $request->param('title');
			$content    = $request->param('content');
			$image      = $request->param('image');
			$lable_name = $request->param('lable_name');
			$time		= time();
		//判断是否艾特专家
		// if(!$zhuanjia){

						

			$data = array();
			$data['title'] = $title;
			$data['content'] = $content;
			$data['image'] = $image;
			$data['lable_name'] = $lable_name;
			$data['time'] = $time;

			//将数据插入数据库
			$res = Db::name('article')->insert($data);
			if(!$res){

				return $this->error("添加数据失败");
			}else{
				return $this->success('新增数据失败','跳转页面路径');
			}

		// }else{

		// 	//艾特专家成为热门问答的帖子
			
		// 	$data = array();
		// 	$data['title'] = $title;
		// 	$data['content'] = $content;
		// 	$data['image'] = $image;
		// 	$data['lable_name'] = $lable_name;
		// 	$data['time'] = $time;
		// 	$data['status'] = 1;

		// 	$res1 = Db::name('article')->insert($data);
		// 	if(!$res1){

		// 		return $this->error("添加数据失败");
		// 	}else{
		// 		return $this->success('新增数据失败','跳转页面路径');
		// 	}
		// }

		
	}

	//帖子点赞
	public function praiseArticle(){
		$request = Request::instance();

		//获取帖子id
		$article_id   = $request->param('article');
		//获取点赞用户的id
		$userid		  = session('userdi');
		//检查用户是否已经点点赞
		$res = Db::name('article_praise')->where('aid','eq',$article_id)->find();
		if(!$res){
				$data1 = array();
				$date1['article_id'] = $article_id;
				$data1['userid']	 = $userid;
				$res1 = Db::name('article_praise')->insert($data1);
		}else{

			echo "您已经点过赞了";
		}
	}

	//帖子取消点赞
	public function deletePraise(){
		$request = Request::instance();

		$userid = $request->session('userid');
		$article_id  = $request->param('article_id');
		//echo 124;die();

		$res = Db::name('article_praise')->where('uid','eq',$userid)->where('aid','eq',$article_id)->delete();

		//var_dump($res);
		if(!$res){

			echo "点赞取消失败";
		}else{
			echo "点赞取消成功";
		}
 }

	//评论帖子
	public function replyArticle(){
		$request = Request::instance();
	
		$article_id  = $request->param('article_id');
		$userid      = $request->session('userid');
		$content     = $request->param('content');
		$time  		 = time();

		$data2 = array();

		$data2['aid'] = $article_id;
		$data2['uid']     = $userid;
		$data2['content']    = $time;

		//将评论内容插入数据库
		$res2 = Db::name('article_content')->insert($data2);
		//var_dump($res2);exit;
		if(!$res){
			$this->error('评论发表失败');
		}else{
			$this->success('成功发表评论','跳转页面路径');
		}



	}
	//收藏帖子
	public function collectionArticle(){
		$request = Request::instance();

		$article_id = $request->param('article_id');
		$userid     = $request->session('userid');

		$data = array();
		$data['aid'] = $article_id;
		$data['uid'] = $userid;

		$res = Db::name('article_collection')->insert($data);

		if(!$res){
			return $this->error('收藏失败');
		}else{
			return $this->success('收藏成功','跳转个人中心路径');
		}

	}


	//取消帖子收藏
	public function deleteCollection(){

		$request = Request::instance();

		$userid = $request->session('userid');

		$article_id = $request->param('article_id');

		$res = Db::name('article_collection')->where('uid','eq',$userid)->where('aid','eq',$article_id)->delete();

		var_dump($res);
		if(!$res){

			echo "取消收藏帖子";
		}else{
			echo "取消失败";
		}
	}

	//@获取所有专家
	public function allExpert(){

		$request = Request::instance();

		$res = Db::name('user')->where('expert','eq',1)->field('username')->select();
		//var_dump($res);
		return $res;
	}

	//帖子详情
	public function articleDetail(){

		$request = Request::instance();
		$article_id = $request->param('article_id');

		$data = Db::name('article')->select();
		//var_dump($data);
		return $data;
	}


}